#include "StdAfx.h"
#include "AudioOut.h"
#include "jsonreader.h"
#include "SubStructure.h"
#include "Menu.h"
#include "Textout.h"
#include "BackgroundImage.h"
#include "SysSceneMusic.h"

SysSceneMusic::SysSceneMusic()
{
	background = new BackgroundImage();
	for(int i = 0; i < 15; ++i)
	{
		MusicName[i] = new Textout();
	}
	MusicInfo = new Textout();
	loadData = jsonreader::Instance();
	audio = AudioOut::Instance();
}

SysSceneMusic::~SysSceneMusic()
{
	delete background;
	delete backMask;
	for(int i = 0; i < 15; ++i)
	{
		delete MusicName[i];
	}
	delete MusicInfo;

	if(SceneBack != NULL)
	{
		SceneBack->Release();
	}
	if(SceneMask != NULL)
	{
		SceneMask->Release();
	}
	if(ConfigTex != NULL)
	{
		ConfigTex->Release();
	}
}

SysSceneState* SysSceneMusic::Instance()
{
	if(p_this == NULL)
	{
		p_this = new SysSceneMusic();
		p_this->Init();
	}
	return p_this;
}

void SysSceneMusic::Init()
{
	TickCount = 0;
	NowState = 0;
	sceneChange = 0;
	changeCount = 0;

	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("music_back.png"), 640, 480, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &SceneBack))
		MessageBox(NULL, _T("music_back.png is not loaded!!!"), _T("ERROR"), MB_OK);

	RECT temp = {0,0,640,480};
	D3DXVECTOR2 tempxy(0.0f, 0.0f);

	background->Setrect(temp);
	background->SetTexture(SceneBack);
	background->SetInvisible(0xffffffff);
	backMask->Setrect(temp);
	backMask->SetTexture(SceneMask);
	backMask->SetInvisible(0xffffffff);

	for(int i = 0; i <15; ++i)
	{
		MusicName[i]->Init();
	}
	MusicInfo->Init();
}

void SysSceneMusic::Draw()
{
	background->Draw();
	for(int i = 0; i < 15; ++i)
	{
		MusicName[i]->Draw();
	}
	backMask->Draw();
	MusicInfo->Draw();
}

void SysSceneMusic::Update(const KeyInput* input)
{
	TickCount++;
	if(TickCount%3 || sceneChange != 0)
	{
		if(sceneChange != 0)
		{
			--changeCount;
			if(changeCount == 0)
			{
				int wParam;
				Event(PRESSED, wParam);
				NowState = 0;
			}
		}
	}
	else
	{
		if(input->VDOWN & 1)
		{
			NowState++;
			if(NowState > 14)
			{
				NowState = 14;
			}
		}
		else if(input->VUP & 1)
		{
			NowState--;
			if(NowState > 999999)
			{
				NowState = 0;
			}
		}
	}
}

void SysSceneMusic::Reset()
{

}